package org.tst.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class LoadWebCompontImpl implements LoadWebCompont {

	@Override
	public String getContent(String url) {

		return this.getHTML(url);
	}


	public String getHTML(String urlToRead) {
		URL url;
		HttpURLConnection conn;
		BufferedReader rd;
		String line;
		String result = "";
		try {
			url = new URL(urlToRead);

			conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(3000);
			conn.connect();

			// int status = conn.getResponseCode();
			// System.out.println(status);
			// switch (status) {
			// case java.net.HttpURLConnection.HTTP_GATEWAY_TIMEOUT:// 504
			// System.out.println("連線網址逾時!");
			// break;
			// case java.net.HttpURLConnection.HTTP_FORBIDDEN:// 403
			// System.out.println("連線網址禁止!");
			// break;
			// case java.net.HttpURLConnection.HTTP_INTERNAL_ERROR:// 500
			// System.out.println("連線網址錯誤或不存在!");
			// break;
			// case java.net.HttpURLConnection.HTTP_NOT_FOUND:// 404
			// System.out.println("連線網址不存在!");
			// break;
			// case java.net.HttpURLConnection.HTTP_OK:
			// System.out.println("OK!");
			// break;
			//
			// }

			// conn.setRequestMethod("GET");
			
//			PrintWriter out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));  
			
			rd = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "UTF-8"));

			while ((line = rd.readLine()) != null) {
				result += line;
			}
			rd.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
